package cn.iocoder.yudao.module.javainfo.controller.admin.financialflow;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.javainfo.controller.admin.financialflow.vo.*;
import cn.iocoder.yudao.module.javainfo.dal.dataobject.financialflow.FinancialFlowDO;
import cn.iocoder.yudao.module.javainfo.service.financialflow.FinancialFlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资金流水")
@RestController
@RequestMapping("/javainfo/financial-flow")
@Validated
public class FinancialFlowController {

    @Resource
    private FinancialFlowService financialFlowService;

    @PostMapping("/create")
    @Operation(summary = "创建资金流水")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:create')")
    public CommonResult<Long> createFinancialFlow(@Valid @RequestBody FinancialFlowSaveReqVO createReqVO) {
        return success(financialFlowService.createFinancialFlow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资金流水")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:update')")
    public CommonResult<Boolean> updateFinancialFlow(@Valid @RequestBody FinancialFlowSaveReqVO updateReqVO) {
        financialFlowService.updateFinancialFlow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资金流水")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:delete')")
    public CommonResult<Boolean> deleteFinancialFlow(@RequestParam("id") Long id) {
        financialFlowService.deleteFinancialFlow(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除资金流水")
                @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:delete')")
    public CommonResult<Boolean> deleteFinancialFlowList(@RequestParam("ids") List<Long> ids) {
        financialFlowService.deleteFinancialFlowListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资金流水")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:query')")
    public CommonResult<FinancialFlowRespVO> getFinancialFlow(@RequestParam("id") Long id) {
        FinancialFlowDO financialFlow = financialFlowService.getFinancialFlow(id);
        return success(BeanUtils.toBean(financialFlow, FinancialFlowRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资金流水分页")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:query')")
    public CommonResult<PageResult<FinancialFlowRespVO>> getFinancialFlowPage(@Valid FinancialFlowPageReqVO pageReqVO) {
        PageResult<FinancialFlowDO> pageResult = financialFlowService.getFinancialFlowPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FinancialFlowRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资金流水 Excel")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFinancialFlowExcel(@Valid FinancialFlowPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FinancialFlowDO> list = financialFlowService.getFinancialFlowPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资金流水.xls", "数据", FinancialFlowRespVO.class,
                        BeanUtils.toBean(list, FinancialFlowRespVO.class));
    }

    @GetMapping("/daily-statistics")
    @Operation(summary = "获取最近x天每天的资金流水统计")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:query')")
    public CommonResult<List<FinancialFlowDailyStatisticsRespVO>> getDailyStatistics(@Valid FinancialFlowStatisticsReqVO reqVO) {
        return success(financialFlowService.getDailyStatistics(reqVO.getDays(), reqVO.getAmountType()));
    }

    @GetMapping("/user-rank")
    @Operation(summary = "获取最近x天用户资金流水排行榜")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:query')")
    public CommonResult<List<FinancialFlowUserRankRespVO>> getUserRank(@Valid FinancialFlowStatisticsReqVO reqVO) {
        return success(financialFlowService.getUserRank(reqVO.getDays(), reqVO.getAmountType(), 10)); // 默认取前10名
    }

    @GetMapping("/user-consumption-stats")
    @Operation(summary = "获取用户消费统计（近一年）")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('javainfo:financial-flow:query')")
    public CommonResult<UserConsumptionStatsRespVO> getUserConsumptionStats(@RequestParam("userId") Long userId) {
        return success(financialFlowService.getUserConsumptionStats(userId));
    }

}